
clear
set more off

/*********************************************************************************
Name: adherence.do

Data In: [Data/Original/patient_surveys_attempts.dta,
		  Data/Original/patient_surveys.dta,
		  Data/Intermediate/patient_controls.dta,
		  Data/Original/patient_start_dates.dta,
		  Data/Original/hw_roster.dta,
		  Data/Intermediate/verified_patients.dta,
		  Data/Original/observation_days_patients.dta,
		  Data/Original/observation_days_centers.dta]

Data Out: [Data/Intermediate/Table3_PanelB_3_sh.dta,
		   Data/Intermediate/adherence.dta,
		   Data/Intermediate/Table3_PanelB_1_sh.dta,
		   Data/Intermediate/Table3_PanelB_2_sh.dta]

Results Out: [Results/Paper/Table3_PanelB_3.out,
			  Results/Appendix/TableC1_PanelB_3.log,
			  Results/Appendix/TableD1_PanelB_3.log,
			  Results/Appendix/TableE1_PanelB_3.out,
			  Results/Appendix/TableB1_PanelB_3.out,
			  Results/Appendix/TableA11_PanelB_3.out,
			  Results/Paper/Table3_PanelB_1.out,
			  Results/Appendix/TableA5_1.out,
			  Results/Appendix/TableC1_PanelB_1.log,
			  Results/Appendix/TableD1_PanelB_1.log,
			  Results/Appendix/TableE1_PanelB_1.out,
			  Results/Appendix/TableB1_PanelB_1.out,
			  Results/Appendix/TableA11_PanelB_1.out,
			  Results/Paper/Table3_PanelB_2.out,
			  Results/Appendix/TableA5_2.out,
			  Results/Appendix/TableC1_PanelB_2.log,
			  Results/Appendix/TableD1_PanelB_2.log,
			  Results/Appendix/TableE1_PanelB_2.out,
			  Results/Appendix/TableB1_PanelB_2.out,
			  Results/Appendix/TableA11_PanelB_2.out] 

Purpose of do-file: Estimating the treatment's impact on treatment adherence

Organization: PART-1: Estimating the treatment's impact on treatment adherence, using patient surveys
			  PART-2: Estimating the treatment's impact on treatment adherence, using observation days
*********************************************************************************/

* Setting path directory
cd "${DIRECTORY}"


****************************************
*** PART-1 *** Estimating the treatment's impact on treatment adherence, using patient surveys
****************************************

** Calling and merging datasets

use "Data/Original/patient_surveys_attempts.dta", clear

merge 1:1 q1_ques_code a02_entry_exit using "Data/Original/patient_surveys.dta"
drop _merge

merge m:1 q1_ques_code using "Data/Intermediate/patient_controls.dta"
drop _merge

merge m:1 q1_ques_code using "Data/Original/patient_start_dates.dta"
drop _merge

merge m:1 Unique_ID UID_Center using "Data/Original/hw_roster.dta"
keep if _merge == 3
drop _merge

merge 1:1 q1_ques_code a02_entry_exit using "Data/Intermediate/verified_patients.dta"
keep if _merge == 3
drop _merge

global patient_controls = "pat_male pat_age pat_caste_general pat_hindu pat_rw_both pat_ownhouse pat_hhd_size pat_migrate_always pat_migrate_6plus pat_time_to_center pat_dum_male pat_dum_age pat_dum_caste_general pat_dum_hindu pat_dum_rw_both pat_dum_ownhouse pat_dum_hhd_size pat_dum_migrate_always pat_dum_migrate_6plus pat_dum_time_to_center" 

global patient_controls_2 = "pat_caste_sc pat_elec pat_migrate_6plus pat_dum_caste_sc pat_dum_elec pat_dum_migrate_6plus"


** Creating the outcome variables

* Occasionally sent someone else to get the pills
ta i13_asha_counselor1
ta i13_latest_provider1
ta i13_asha_counselor1 i13_latest_provider1
gen send_someone_else = (i13_asha_counselor1 == 1 | i13_latest_provider1 == 1) if i13_asha_counselor1 ~= . | i13_latest_provider1 ~= .
label var send_someone_else "Occasionally sent someone else to get the pills"

* Health worker gave medicine for a week or longer duration at the same time
ta k14_oa_medici_week survey_complete,m 
gen med_week_or_longer = (k14_oa_medici_week == 1 | k14_oa_medici_week == 2 | k14_oa_medici_week == 3) if k14_oa_medici_week ~= . & k14_oa_medici_week ~= -999
label var med_week_or_longer "Took medicine for a week or longer duration at the same time"


** Regressions

* Defining sample: all verified patients, and using their last survey

gen sample_temp = last_complete == 1 & verified_pat == 1

* Estimating the treatment's impact on treatment adherence, using patient surveys: Table 3, Panel B, columns 5-8

* Sidak-Holm correction

preserve

foreach y in send_someone_else med_week_or_longer{
	ivreg2 `y' treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey if sample_temp == 1, small cl(uid_cluster)   
	test treatment
	gen `y'_p1 = `r(p)'
	ivreg2 `y' treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey $patient_controls if sample_temp == 1, small cl(uid_cluster)   
	test treatment
	gen `y'_p2 = `r(p)'
}	

keep send_someone_else_p* med_week_or_longer_p*
gen id = 99
keep in 1
reshape long send_someone_else_p med_week_or_longer_p, i(id) j(test) 
drop id
xpose, clear varname
drop in 1
rename v1 pval_1
rename v2 pval_2
rename _varname variable
save "Data/Intermediate/Table3_PanelB_3_sh.dta", replace	

restore

su send_someone_else if treatment == 0 & sample_temp == 1
local mean_control=r(mean)

ivreg2 send_someone_else treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table3_PanelB_3.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 send_someone_else treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey $patient_controls if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table3_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

su med_week_or_longer if treatment == 0 & sample_temp == 1
local mean_control=r(mean)

ivreg2 med_week_or_longer treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table3_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)
	
ivreg2 med_week_or_longer treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey $patient_controls if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table3_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

* Wild cluster bootstrap or pairs cluster bootstrap: Table C1, Panel B, columns 5-8, and Table D1, Panel B, columns 5-8

preserve 

keep if last_complete == 1 & verified_pat == 1

foreach var in _male _age _caste_general _hindu _rw_both _ownhouse _hhd_size _migrate_always _migrate_6plus _time_to_center _dum_male _dum_age _dum_caste_general _dum_hindu _dum_rw_both _dum_ownhouse _dum_hhd_size _dum_migrate_always _dum_migrate_6plus _dum_time_to_center {
rename pat`var' `var'
}

global patient_controls_b = "_male _age _caste_general _hindu _rw_both _ownhouse _hhd_size _migrate_always _migrate_6plus _time_to_center _dum_male _dum_age _dum_caste_general _dum_hindu _dum_rw_both _dum_ownhouse _dum_hhd_size _dum_migrate_always _dum_migrate_6plus _dum_time_to_center"

* wild cluster bootstrap

log using "Results/Appendix/TableC1_PanelB_3.log", replace
foreach var in send_someone_else med_week_or_longer{
wildbootstrap regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey, cluster(uid_cluster) reps(5000) rseed(13915183)
wildbootstrap regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey $patient_controls_b, cluster(uid_cluster) reps(5000) rseed(13915183)
}
log close

* pairs cluster bootstrap

log using "Results/Appendix/TableD1_PanelB_3.log", replace
foreach var in send_someone_else med_week_or_longer{
clustse regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
clustse regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey $patient_controls_b, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
}
log close

restore

* With second set of controls: Table E1, Panel B, columns 5-8

su send_someone_else if treatment == 0 & sample_temp == 1
local mean_control=r(mean)

ivreg2 send_someone_else treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE1_PanelB_3.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 send_someone_else treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey $patient_controls_2 if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE1_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

su med_week_or_longer if treatment == 0 & sample_temp == 1
local mean_control=r(mean)

ivreg2 med_week_or_longer treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE1_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)
	
ivreg2 med_week_or_longer treatment final_stratum_id1-final_stratum_id13 post_exp entry_survey $patient_controls_2 if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE1_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

* Restricting the sample to patients detected before experiment start date: Table B1, Panel B, columns 5-8

su send_someone_else if treatment == 0 & sample_temp == 1 & post_exp == 0
local mean_control=r(mean)

ivreg2 send_someone_else treatment final_stratum_id1-final_stratum_id13 entry_survey if sample_temp == 1 & post_exp == 0, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableB1_PanelB_3.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 send_someone_else treatment final_stratum_id1-final_stratum_id13 entry_survey $patient_controls if sample_temp == 1 & post_exp == 0, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableB1_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

su med_week_or_longer if treatment == 0 & sample_temp == 1 & post_exp == 0
local mean_control=r(mean)
	
ivreg2 med_week_or_longer treatment final_stratum_id1-final_stratum_id13 entry_survey if sample_temp == 1 & post_exp == 0, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableB1_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 med_week_or_longer treatment final_stratum_id1-final_stratum_id13 entry_survey $patient_controls if sample_temp == 1 & post_exp == 0, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableB1_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

* Change in treatment impact over time: Table A11, Panel B, columns 5-8

gen time_from_exp = (treatment_start_date - expstartdate) / 100
gen treatment_time = treatment * time_from_exp

su send_someone_else if treatment == 0 & sample_temp == 1
local mean_control=r(mean)

ivreg2 send_someone_else treatment treatment_time time_from_exp final_stratum_id1-final_stratum_id13 entry_survey post_exp if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment treatment_time using "Results/Appendix/TableA11_PanelB_3.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment treatment_time)

ivreg2 send_someone_else treatment treatment_time time_from_exp final_stratum_id1-final_stratum_id13 entry_survey post_exp $patient_controls if sample_temp == 1, small cl(uid_cluster) 
quietly outreg2 treatment treatment_time using "Results/Appendix/TableA11_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment treatment_time)

su med_week_or_longer if treatment == 0 & sample_temp == 1
local mean_control=r(mean)
	
ivreg2 med_week_or_longer treatment treatment_time time_from_exp final_stratum_id1-final_stratum_id13 entry_survey post_exp if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment treatment_time using "Results/Appendix/TableA11_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment treatment_time)

ivreg2 med_week_or_longer treatment treatment_time time_from_exp final_stratum_id1-final_stratum_id13 entry_survey $patient_controls post_exp if sample_temp == 1, small cl(uid_cluster)  
quietly outreg2 treatment treatment_time using "Results/Appendix/TableA11_PanelB_3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment treatment_time)


****************************************
*** PART-2 *** Estimating the treatment's impact on treatment adherence, using observation days
****************************************

** Calling and merging datasets

use "Data/Original/patient_surveys_attempts.dta", clear

merge m:1 q1_ques_code using "Data/Intermediate/patient_controls.dta"
drop _merge

merge m:1 q1_ques_code using "Data/Original/patient_start_dates.dta"
drop _merge

merge m:1 Unique_ID UID_Center using "Data/Original/hw_roster.dta"
keep if _merge == 3
drop _merge

merge 1:1 q1_ques_code a02_entry_exit using "Data/Intermediate/verified_patients.dta"
keep if _merge == 3
drop _merge

so q1_ques_code a02_entry_exit
by q1_ques_code: gen id = _n
keep if id == 1 
drop id

merge 1:m q1_ques_code using "Data/Original/observation_days_patients.dta"
drop _merge

label variable pat_visit_starttime_hh "Start time of patient visit [only mobile mon]"
label variable pat_visit_starttime_mm "Start time of patient visit [only mobile mon]"
label variable pat_picked_pills_yn "Did the patient pick the pills?"
label define yesno_0 0 "NO" 1 "YES" ///
          .b "MISSING VAL/BLANK" ///
          .i "INVALID SKIP" ///
          .m "MISSING" ///
          .n "NOT APPLICABLE IN THIS FORM" ///
          .o "NO LABEL" ///
          .p "PII REMOVED" ///
          .s "VALID SKIP" ///
          .v "VAL NOT ACCEPTED"

preserve

	use "Data/Original/observation_days_centers.dta", clear
	keep unique_mon_instance visit_date
	
	so unique_mon_instance
	by unique_mon_instance: gen id = _n
	keep if id == 1 
	drop id

	tempfile monitoring_date
	save `monitoring_date'

restore

merge m:1 unique_mon_instance using `monitoring_date'
drop if q1_ques_code == .

* Creating variables indicating whether the visit took place before the beginning of the experiment or during it
gen during_exp = visit_date >= expstartdate & !missing(patient_row)

global patient_controls = "pat_male pat_age pat_caste_general pat_hindu pat_rw_both pat_ownhouse pat_hhd_size pat_migrate_always pat_migrate_6plus pat_time_to_center pat_dum_male pat_dum_age pat_dum_caste_general pat_dum_hindu pat_dum_rw_both pat_dum_ownhouse pat_dum_hhd_size pat_dum_migrate_always pat_dum_migrate_6plus pat_dum_time_to_center"

global patient_controls_2 = "pat_caste_sc pat_elec pat_migrate_6plus pat_dum_caste_sc pat_dum_elec pat_dum_migrate_6plus"


** Creating the outcome variables

* Health worker met with patients
gen pat_couns_meet = (pat_came_yn == 1 | pat_at_home_yn == 1) if !(missing(pat_came_yn) & missing(pat_at_home_yn))

* Met with relatives (not available for paper forms)
gen reltv_couns_meet = (reltv_came_yn == 1 | reltv_at_home_yn == 1) if !(missing(reltv_came_yn) & missing(reltv_at_home_yn))

* Indicator variable for patient's biometric information
replace pat_biom_yn = 0 if pat_biom_yn == 2

* Variable for patient taking pills (different from "picking" pills)
replace pat_took_pills_yn = 0 if pat_took_pills_yn == 2

* Variable for patient/relative picking pills (different from "taking" pills)
replace pat_picked_pills_yn = 0 if pat_picked_pills_yn == 2
replace reltv_picked_pills_yn = . if reltv_picked_pills_yn == 2

* Labelling values of new variables
label values pat_couns_meet reltv_couns_meet ///
			pat_biom_yn ///
			pat_took_pills_yn pat_picked_pills_yn reltv_picked_pills_yn ///
			yesno_0

egen extra_info_pat = anycount(pat_biom_yn pat_took_pills_yn num_pills_pat_took pat_picked_pills_yn num_pills_pat_pickd), values(1/95)
egen extra_info_reltv = anycount(reltv_picked_pills_yn num_pills_reltv_pickd), values(1/86)

replace pat_couns_meet = 1 if extra_info_pat >= 2 & (observation_type == 1 | observation_type == 4)
replace reltv_couns_meet = 1 if extra_info_reltv >= 2 & (observation_type == 1 | observation_type == 4)
replace pat_couns_meet = 1 if (extra_info_pat >= 2 | extra_info_reltv >= 2) & (observation_type == 2 | observation_type == 3)

* Creating variables about home visits

gsort unique_mon_instance q1_ques_code -patient_row
gen hv_match_pat_id = 1 if (q1_ques_code[_n] ==  q1_ques_code[_n - 1]) & !missing(q1_ques_code) & ///
							(unique_mon_instance[_n] == unique_mon_instance[_n - 1]) & ///
							(patient_row == 1) & (patient_row[_n - 1] == 2)

foreach var of varlist pat_took_pills_yn pat_picked_pills_yn reltv_picked_pills_yn {
		
		gen `var'_hv = `var'[_n-1] if hv_match_pat_id == 1
}

* Creating metrics to evaluate patient adherence

* Aggregating patient visits where patient made 2 visits in the same day

sort q1_ques_code unique_mon_instance patient_row
by   q1_ques_code unique_mon_instance patient_row: gen temp_n = _n

foreach var of varlist  pat_couns_meet pat_took_pills_yn pat_picked_pills_yn reltv_couns_meet reltv_picked_pills_yn ///
						pat_took_pills_yn_hv pat_picked_pills_yn_hv reltv_picked_pills_yn_hv {
	cap drop `var'_temp
	by q1_ques_code unique_mon_instance patient_row: egen `var'_temp = max(`var')
	tab  `var'  `var'_temp, m nola
	
	replace `var' = `var'_temp if `var' != `var'_temp 
	drop `var'_temp
}

keep if temp_n == 1
drop temp_n

* [adherence_1] Patient was met on the designated date, and took or picked up the pill himself
gen adherence_1 = 0
replace adherence_1 = 1 if (pat_couns_meet == 1 & (pat_took_pills_yn == 1 | pat_picked_pills_yn == 1))
replace adherence_1 = . if (missing(pat_couns_meet)) | ///
						(pat_couns_meet == 1) & !(pat_took_pills_yn == 1 | pat_picked_pills_yn == 1 | (pat_took_pills_yn == 0 & pat_picked_pills_yn == 0))
// Coding "adherence_1" to "missing" whenever either we don't know if the patient was met or not, OR when we know that patient was met but don't know definitively that patient did not take or pick a pill. 
							
* [adherence_2] adherence_1 + relative was met on the designated date, and they picked a pill on patient’s behalf + patient or relative was met during a home visit

gen adherence_2 = adherence_1
replace adherence_2 = . if (adherence_1 == 0 & missing(reltv_picked_pills_yn) & reltv_couns_meet != 0 & inlist(observation_type,1,4))
replace adherence_2 = 1 if reltv_picked_pills_yn == 1 | pat_took_pills_yn_hv == 1 | pat_picked_pills_yn_hv == 1 | reltv_picked_pills_yn_hv == 1
// Logically, we cannot code adherence_2 = 0 if adherence_1 == MISSING (We don't definitively know that patient did not take their pill, how can we definitively know that neither patient nor relative took a pill?)
// However, it is possible that where we know adherence_1 == 0, we might not be able to definitively say that a relative did not show up either, and hence adherence_2 == .

// Not coding adherence_2 as MISSING when HV variables are missing. This is because HV variables are coded as missing for almost all patients, indicating that the visit was not made (not that the visit was not recorded, and hence information missing)						 

* Saving the dataset for use in hw_effort.do
save "Data/Intermediate/adherence.dta", replace

* Checking scope of baseline data available

bys UID_Center q1_ques_code visit_date: egen pat_row_min = min(patient_row)
replace pat_row_min = . if pat_row_min != patient_row

bys UID_Center q1_ques_code (visit_date): egen visit_number_pat = seq()  if !missing(pat_row_min),from(1) to(30) 


** Identifying compliers in observation days

/* Rationale for assigning "complier":
1. Official definition of "default" by WHO: (A) who either missed pills for more than 60 days anytime during the treatment;  OR (B) who stopped taking pills before 120 days of the treatment as defaults.
2. We can't identify defaults of type-B, since we are not monitoring every day. Basically, if we don't observe a patient for any of the 120 days since his start of treatment, we can't definitively say that the patient did not take pills - we could have systematically been missing them because of our monitoring schedule.
3. To identify type-A defaults also, we need continuous observation of patients. If we observe patients 60 days apart and both times they have skipped a pill, we can't say with certainty that they missed pills for consecutive 60 days. Our median duration between observations is 18 days, meaning there are lots of opportunity for patients to take pills even if we did not observe them doing so.
4. The only thing we can do is to identify patients who definitely did not default. These are patients who we have always observed take pills at some point in a 60-day period AND who continue their pill intake beyond 120 days of treatment start.*/

/* Operationalization: 
a/ sort observations by patient, dummy = 1 if patient_row == 1 or 3, adherence_1 (and we'll only consider observations with dummy = 1 and adherence_1 = 1 below)
b/ compute the distance between any 2 dates for these observations: if higher than 60, then possible default
c/ compute the distance between first date with adherence_1 = 1 and treatment start: if higher than 60, then possible default
d/ compute the distance between treatment start and last date with adherence_1 = 1: if lower then 120, then possible default
e/ if none of the possible default variables is equal to 1, then we confirm that there was no default*/

* Creating dummy variable for patient_row = 1 or 3
gen main_visit = inlist(patient_row,1,3) if !missing(patient_row)

* Creating variable for "Adherence_1" compliance
gsort q1_ques_code -main_visit -adherence_1 visit_number_pat 
// For each patient, all main visits with Adherence_1 == 1 are now sorted first.

* (a) Generating variable to calculate days between consecutive adherence_1 = 1 visits
gen days_between_adh_1 = visit_date - visit_date[_n-1] if q1_ques_code == q1_ques_code[_n-1] & adherence_1 == 1 & adherence_1[_n-1] == 1 & main_visit == 1
replace days_between_adh_1 = max(0,visit_date - treatment_start_date) if days_between_adh_1 == . & adherence_1 == 1 & !missing(visit_date) & main_visit == 1 
// Finding number of days between treatment start and first pill (basically, initial lag for first adherence-1 == 1)
by q1_ques_code: egen max_days_between_adh_1 = max(days_between_adh_1)

* (b) Generating variable to calculate total period of adherence_1
by q1_ques_code: egen last_date_adh_1 = max(visit_date) if adherence_1 == 1 & main_visit == 1
gen trt_days_adh_1 = last_date_adh_1 - treatment_start_date

* (c) Generating overall compliance status
gen complier_adh_1_temp = (max_days_between_adh_1 <= 60 & trt_days_adh_1 >= 120) if !missing(max_days_between_adh_1) & !missing(trt_days_adh_1)
by q1_ques_code: egen complier_adh_1 = max(complier_adh_1_temp) 
// Generating compliance status for all rows for each patient

* (d) Some patients never have adherence_1 = 1, so their compliance status is generated as ".". Replacing with 0 where at least some visit has adherence_1 == 0. 
by q1_ques_code: egen adherence_1_temp = max(adherence_1) if (main_visit == 1)
replace complier_adh_1 = 0 if adherence_1_temp == 0 & complier_adh_1 == .
drop adherence_1_temp complier_adh_1_temp

* Creating variable for "Adherence_2" compliance
gsort q1_ques_code -main_visit -adherence_2 visit_number_pat 
// For each patient, all "main visits" with Adherence_2 == 1 are now sorted first.

* (a) Generating variable to calculate days between consecutive adherence_2 = 1 visits
gen days_between_adh_2 = visit_date - visit_date[_n-1] if q1_ques_code == q1_ques_code[_n-1] & adherence_2 == 1 & adherence_2[_n-1] == 1 & main_visit == 1
replace days_between_adh_2 = max(0,visit_date - treatment_start_date) if days_between_adh_2 == . & adherence_2 == 1 & !missing(visit_date) & main_visit == 1 
// Finding number of days between treatment start and first pill (basically, initial lag for first adherence_2 == 1)
by q1_ques_code: egen max_days_between_adh_2 = max(days_between_adh_2)

* (b) Generating variable to calculate total period of adherence_2
by q1_ques_code: egen last_date_adh_2 = max(visit_date) if adherence_2 == 1 & main_visit == 1
gen trt_days_adh_2 = last_date_adh_2 - treatment_start_date

* (c) Generating overall compliance status
gen complier_adh_2_temp = (max_days_between_adh_2 <= 60 & trt_days_adh_2 >= 120) if !missing(max_days_between_adh_2) & !missing(trt_days_adh_2)
by q1_ques_code: egen complier_adh_2 = max(complier_adh_2_temp) 
// Generating compliance status for all rows for each patient

* (d) Some patients never have adherence_2 = 1, so their compliance status is generated as ".". Replacing with 0 where at least some visit has adherence_2 == 0.
by q1_ques_code: egen adherence_2_temp = max(adherence_2) if (main_visit == 1)
replace complier_adh_2 = 0 if adherence_2_temp == 0 & complier_adh_2 == . 
// Some patients never have adherence_2 = 1, so their compliance status is generated as ".". Replacing with 0 where at least some visit has adherence_2 == 0.
drop adherence_2_temp complier_adh_2_temp


** Estimating the treatment's impact on treatment adherence, using observation days

* Main regressions: Table 3, Panel B, columns 1 and 2, and Table A5, columns 1 and 2

bys q1_ques_code: gen pat_id = _n if !missing(q1_ques_code)

* Sidak-Holm correction

preserve

foreach y in adherence_1{
	ivreg2 `y' treatment final_stratum_id1-final_stratum_id13 post_exp if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
	test treatment
	gen `y'_p1 = `r(p)'
	ivreg2 `y' treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
	test treatment
	gen `y'_p2 = `r(p)'
}	

keep adherence_1_p*
gen id = 99
keep in 1

reshape long adherence_1_p, i(id) j(test) 
drop id
xpose, clear varname
drop in 1
rename v1 pval_1
rename v2 pval_2
rename _varname variable

save "Data/Intermediate/Table3_PanelB_1_sh.dta", replace
	
restore

su adherence_1 if treatment == 0 & verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1
local mean_control=r(mean)

ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 post_exp if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table3_PanelB_1.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table3_PanelB_1.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

su adherence_2 if treatment == 0 & verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1
local mean_control=r(mean)
	
ivreg2 adherence_2 treatment final_stratum_id1-final_stratum_id13 post_exp if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA5_1.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 adherence_2 treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA5_1.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

* "Transformed" Lee bounds: reported in Section 4.1, footnote 17 in the text, and in the notes of Table 3

preserve

keep if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1 & treatment ~= .
count if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1 & treatment == 1
* 6082 observations in treatment group; we need to drop the excess: .3695937 / (.3695937 + 3.57438) = 0.093710995 (numbers come from Table A2, Panel a, column 5 -- data_availability.do) -- thus, we need to drop 0.093710995 * 6082 = 570 observations
so q1_ques_code unique_mon_instance patient_row
set seed 35156346
gen temp = uniform()
so treatment adherence_1 temp
by treatment adherence_1: gen temp2 = _n
count if treatment == 1 & adherence_1 == 1
assert r(N) > 570
replace adherence_1 = . if treatment == 1 & adherence_1 == 1 & temp2 <= 570
ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 post_exp if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   

restore

preserve

keep if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1 & treatment ~= .
count if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1 & treatment == 1
* 6082 observations in treatment group; we need to drop the excess: .3695937 / (.3695937 + 3.57438) = 0.093710995 (numbers come from Table A2, Panel a, column 5 -- data_availability.do) -- thus, we need to drop 0.093710995 * 6082 = 570 observations
so q1_ques_code unique_mon_instance patient_row
set seed 35156346
gen temp = uniform()
so treatment adherence_1 temp
by treatment adherence_1: gen temp2 = _n
count if treatment == 1 & adherence_1 == 0
assert r(N) > 570
replace adherence_1 = . if treatment == 1 & adherence_1 == 0 & temp2 <= 570
ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 post_exp if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   

restore

* Wild cluster bootstrap or pairs cluster bootstrap: Table C1, Panel B, columns 1 and 2, and Table D1, Panel B, columns 1 and 2

preserve

keep if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1

foreach var in _male _age _caste_general _hindu _rw_both _ownhouse _hhd_size _migrate_always _migrate_6plus _time_to_center _dum_male _dum_age _dum_caste_general _dum_hindu _dum_rw_both _dum_ownhouse _dum_hhd_size _dum_migrate_always _dum_migrate_6plus _dum_time_to_center {
rename pat`var' `var'
}

global patient_controls_b = "_male _age _caste_general _hindu _rw_both _ownhouse _hhd_size _migrate_always _migrate_6plus _time_to_center _dum_male _dum_age _dum_caste_general _dum_hindu _dum_rw_both _dum_ownhouse _dum_hhd_size _dum_migrate_always _dum_migrate_6plus _dum_time_to_center"

* wild cluster bootstrap

log using "Results/Appendix/TableC1_PanelB_1.log", replace
foreach var in adherence_1{
wildbootstrap regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp, cluster(uid_cluster) reps(5000) rseed(13915183)
wildbootstrap regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls_b, cluster(uid_cluster) reps(5000) rseed(13915183)
}
log close

* pairs cluster bootstrap

log using "Results/Appendix/TableD1_PanelB_1.log", replace
foreach var in adherence_1{
clustse regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
clustse regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls_b, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
}
log close

restore

* With second set of controls: Table E1, Panel B, columns 1 and 2

su adherence_1 if treatment == 0 & verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1
local mean_control=r(mean)

ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 post_exp if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE1_PanelB_1.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls_2 if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE1_PanelB_1.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

* Restricting the sample to patients detected before experiment start date: Table B1, Panel B, columns 1 and 2

su adherence_1 if treatment == 0 & verified_pat == 1 & inlist(patient_row,1,3) & post_exp == 0 & during_exp == 1
local mean_control=r(mean)
	
ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 if verified_pat == 1 & inlist(patient_row,1,3) & post_exp == 0 & during_exp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableB1_PanelB_1.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)
	
ivreg2 adherence_1 treatment final_stratum_id1-final_stratum_id13 $patient_controls if verified_pat == 1 & inlist(patient_row,1,3)  & post_exp == 0 & during_exp == 1, small cl(uid_cluster) 
quietly outreg2 treatment using "Results/Appendix/TableB1_PanelB_1.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

* Change in treatment impact over time: Table A11, Panel B, columns 1 and 2

gen time_from_exp = (visit_date - expstartdate) / 100
gen treatment_time = treatment * time_from_exp

su adherence_1 if treatment == 0 & verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1
local mean_control=r(mean)
	
ivreg2 adherence_1 treatment treatment_time time_from_exp final_stratum_id1-final_stratum_id13 post_exp if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster) 
quietly outreg2 treatment treatment_time using "Results/Appendix/TableA11_PanelB_1.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment treatment_time)
	
ivreg2 adherence_1 treatment treatment_time time_from_exp final_stratum_id1-final_stratum_id13 $patient_controls post_exp if verified_pat == 1 & inlist(patient_row,1,3) & during_exp == 1, small cl(uid_cluster)   
quietly outreg2 treatment treatment_time using "Results/Appendix/TableA11_PanelB_1.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment treatment_time)


** Estimating the treatment's impact on overall treatment compliance, using observation days

* Main regressions: Table 3, Panel B, columns 3 and 4, and Table A5, columns 3 and 4

* Sidak-Holm correction

preserve

foreach y in complier_adh_1 {
	ivreg2 `y' treatment final_stratum_id1-final_stratum_id13 post_exp if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
	test treatment
	gen `y'_p1 = `r(p)'
	ivreg2 `y' treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
	test treatment
	gen `y'_p2 = `r(p)'
}	

keep complier_adh_1_p* 
gen id = 99
keep in 1

reshape long complier_adh_1_p, i(id) j(test) 
drop id
xpose, clear varname
drop in 1
rename v1 pval_1
rename v2 pval_2
rename _varname variable

save "Data/Intermediate/Table3_PanelB_2_sh.dta", replace
	
restore

su complier_adh_1 if treatment == 0 & pat_id == 1 & verified_pat == 1
local mean_control=r(mean)

ivreg2 complier_adh_1 treatment final_stratum_id1-final_stratum_id13 post_exp if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table3_PanelB_2.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 complier_adh_1 treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table3_PanelB_2.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

su complier_adh_2 if treatment == 0  & pat_id == 1 & verified_pat == 1
local mean_control=r(mean)
	
ivreg2 complier_adh_2 treatment final_stratum_id1-final_stratum_id13 post_exp if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA5_2.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)
	
ivreg2 complier_adh_2 treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA5_2.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

* Wild cluster bootstrap or pairs cluster bootstrap: Table C1, Panel B, columns 3 and 4, and Table D1, Panel B, columns 3 and 4

preserve

keep if pat_id == 1 & verified_pat == 1

foreach var in _male _age _caste_general _hindu _rw_both _ownhouse _hhd_size _migrate_always _migrate_6plus _time_to_center _dum_male _dum_age _dum_caste_general _dum_hindu _dum_rw_both _dum_ownhouse _dum_hhd_size _dum_migrate_always _dum_migrate_6plus _dum_time_to_center {
rename pat`var' `var'
}

global patient_controls_b = "_male _age _caste_general _hindu _rw_both _ownhouse _hhd_size _migrate_always _migrate_6plus _time_to_center _dum_male _dum_age _dum_caste_general _dum_hindu _dum_rw_both _dum_ownhouse _dum_hhd_size _dum_migrate_always _dum_migrate_6plus _dum_time_to_center"

* wild cluster bootstrap

log using "Results/Appendix/TableC1_PanelB_2.log", replace
foreach var in complier_adh_1{
wildbootstrap regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp, cluster(uid_cluster) reps(5000) rseed(13915183)
wildbootstrap regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls_b, cluster(uid_cluster) reps(5000) rseed(13915183)
}
log close

* pairs cluster bootstrap

log using "Results/Appendix/TableD1_PanelB_2.log", replace
foreach var in complier_adh_1{
clustse regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
clustse regress `var' treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls_b, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
}
log close

restore

* With second set of controls: Table E1, Panel B, columns 3 and 4

su complier_adh_1 if treatment == 0 & pat_id == 1 & verified_pat == 1
local mean_control=r(mean)

ivreg2 complier_adh_1 treatment final_stratum_id1-final_stratum_id13 post_exp if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE1_PanelB_2.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 complier_adh_1 treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls_2 if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE1_PanelB_2.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

* Restricting the sample to patients detected before experiment start date: Table B1, Panel B, columns 3 and 4

su complier_adh_1 if treatment == 0 & pat_id == 1 & verified_pat == 1 & post_exp == 0 
local mean_control=r(mean)

ivreg2 complier_adh_1 treatment final_stratum_id1-final_stratum_id13 if pat_id == 1 & verified_pat == 1 & post_exp == 0 , small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableB1_PanelB_2.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 complier_adh_1 treatment final_stratum_id1-final_stratum_id13 $patient_controls if pat_id == 1 & verified_pat == 1 & post_exp == 0 , small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableB1_PanelB_2.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

* Change in treatment impact over time: Table A11, Panel B, columns 3 and 4

drop time_from_exp treatment_time
gen time_from_exp = (treatment_start_date - expstartdate) / 100
gen treatment_time = treatment * time_from_exp

su complier_adh_1 if treatment == 0 & pat_id == 1 & verified_pat == 1
local mean_control=r(mean)

ivreg2 complier_adh_1 treatment treatment_time time_from_exp final_stratum_id1-final_stratum_id13 post_exp if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA11_PanelB_2.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment treatment_time)

ivreg2 complier_adh_1 treatment treatment_time time_from_exp final_stratum_id1-final_stratum_id13 post_exp $patient_controls if pat_id == 1 & verified_pat == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA11_PanelB_2.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment treatment_time)


** Sidak-Holm correction: combining all the outcomes included in Table 3, Panel B

use "Data/Intermediate/Table3_PanelB_1_sh.dta", clear
append using "Data/Intermediate/Table3_PanelB_2_sh.dta"
append using "Data/Intermediate/Table3_PanelB_3_sh.dta"

gen number = _N
foreach y in  pval_1 pval_2 {
	sort `y'
	gen `y'_k=(_N+1)-_n	
	generate `y'_sidak=1-(1-`y')^`y'_k
	replace  `y'_sidak=`y'_sidak[_n-1] if `y'_sidak[_n-1]>`y'_sidak in 2/L
	replace  `y'_sidak=1 if `y'_sidak>1 & `y'_sidak~=.
	drop `y'_k
}
drop number
order variable pval_1* pval_2*
